package com.zhitong.ztblog.services.servicesImp;

import com.zhitong.ztblog.entity.User;
import com.zhitong.ztblog.mappers.UserMapper;
import com.zhitong.ztblog.services.IUserService;
import com.zhitong.ztblog.utils.AjaxResponse;
import com.zhitong.ztblog.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        User user = userMapper.findByUsername(username);
      return user;
    }

    @Override
    public User findById(int id) {
        User user = userMapper.findById(id);
        return user;
    }

    @Override
    public void insertUser(User user) {
        userMapper.insertUser(user);
    }

    @Override
    public AjaxResponse login(User user) throws Exception {
        User user1 = findByUsername(user.getUserName());
        if (user1==null){
            AjaxResponse a=new AjaxResponse();
            a.setCode(500);
            a.setMessage("用户不存在");
            return a;
        }
        if (MD5.checkPW(user.getPassword(),user1.getPassword())){
            AjaxResponse success = AjaxResponse.success(user1.getId());
            success.setMessage("登录成功");
            return success;
        }else{
            AjaxResponse a=new AjaxResponse();
            a.setCode(500);
            a.setMessage("密码错误");
            return a;
        }

    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }
}
